async function startWebRTC() {
    const pc = new RTCPeerConnection();

    // 显示远端视频
    pc.ontrack = function(event) {
        const video = document.getElementById('webrtc-video');
        if (video.srcObject !== event.streams[0]) {
            video.srcObject = event.streams[0];
        }
    };

    // 创建 offer，发送给后端
    const offer = await pc.createOffer();
    await pc.setLocalDescription(offer);

    const resp = await fetch('/offer', {
        method: 'POST',
        headers: {'Content-Type': 'application/json'},
        body: JSON.stringify({
            sdp: pc.localDescription.sdp,
            type: pc.localDescription.type
        })
    });
    const answer = await resp.json();
    await pc.setRemoteDescription(answer);
}

window.onload = startWebRTC;